using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;
using System.Drawing;
using MacomberMap.Common.User_Interfaces.Components;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using MacomberMap.Common.Components;
using MacomberMap.Common.Types;
using MacomberMap.Common.Database_Interface;

namespace MacomberMap.Common.User_Interfaces.One_Lines.Elements
{
    /// <summary>
    /// This class handles the components for a Disconnector
    /// </summary>
    public class MM_OneLine_Switch : MM_OneLine_Element
    {
        #region Variable declarations
       
        /// <summary>Whether the breaker is opened</summary>
        [Category("MacomberMap"), Description("Whether the switch is opened"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public CheckState Opened
        {
            get { return _Opened; }
            set { _Opened = value; this.Invalidate(); }
        }
        private CheckState _Opened = CheckState.Indeterminate;
        

        /// <summary>Whether SCADA reports the breaker as opened</summary>
        [Category("MacomberMap"), Description("Whether SCADA reports the switch as opened"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public CheckState ScadaOpened
        {
            get { return _ScadaOpened; }
            set { _ScadaOpened = value; this.Invalidate(); }
        }
        private CheckState _ScadaOpened = CheckState.Indeterminate;        
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new viewable line
        /// </summary>
        /// <param name="ElemConfig">The Xml configuration for the line</param>
        /// <param name="DisplayParameters">The display parameters for the line</param>
        /// <param name="DbConnection">The database connection</param>
        public MM_OneLine_Switch(XmlElement ElemConfig, MM_OneLine_Display DisplayParameters, MM_Database DbConnection)
            : base(ElemConfig, DisplayParameters, DbConnection)
        {
            //Retrieve our arrow direction from the 
            Orientation = ParseOrientation(ElemConfig.Attributes["Orientation"].Value);

            
        }

        /// <summary>
        /// Initialize a new switch
        /// </summary>
        /// <param name="DisplayParameters"></param>
        public MM_OneLine_Switch(MM_OneLine_Display DisplayParameters)
            : base(DisplayParameters)
        {
        }
        /// <summary>
        /// Initialize a new viewable switch
        /// </summary>
        /// <param name="SelectedElement">The CIM element upon which the switch is based</param>
        /// <param name="DisplayParameters">The display parameters</param>
        /// <param name="Orientation">The orientation of the switch</param>
        public MM_OneLine_Switch(MM_Element SelectedElement, MM_OneLine_Display DisplayParameters, MM_OneLine_Element.enumOrientations Orientation)
            : base(SelectedElement, DisplayParameters, Orientation)
        {
                            
        }
        #endregion

        #region Rendering
        /// <summary>
        /// Draw the switch
        /// </summary>
        /// <param name="g"></param>
        public override void DrawImage(Graphics g)
        {

            //First, clear the background
            using (Brush br = new SolidBrush(BackColor))
                g.FillRectangle(br, DisplayRectangle);

            bool IsVertical = Orientation == enumOrientations.Vertical || Orientation == enumOrientations.Up || Orientation == enumOrientations.Down;
            Point Center = CenterRect(new Rectangle(Point.Empty, DisplayRectangle.Size));
            Size SlashSize = new Size(6, 6);

            //Now, build our drawing brush
            using (Pen DrawPen = new Pen(KVLevel == null ? Color.Red : KVLevel.Energized.ForeColor))
            {
                if (_Opened == CheckState.Indeterminate)
                {
                    DrawPen.DashStyle = DashStyle.Dot;
                    using (Font BoldFont = new Font("Arial", 14, FontStyle.Bold))
                    using (SolidBrush DrawBrush = new SolidBrush(DrawPen.Color))
                        g.DrawString("?", BoldFont, DrawBrush, this.DisplayRectangle, CenterFormat);
                }


                if (_Opened == CheckState.Unchecked)
                {
                    g.DrawLine(DrawPen, Point.Subtract(Center, SlashSize), Point.Add(Center, SlashSize));
                    if (IsVertical)
                        g.DrawLine(DrawPen, Center.X, 0, Center.X, Height - 1);
                    else
                        g.DrawLine(DrawPen, 0, Center.Y, Width - 1, Center.Y);
                }
                else if (IsVertical)
                {
                    g.DrawLines(DrawPen, IntToPoints(Center.X, 0, Center.X, Center.Y - SlashSize.Height, Center.X + SlashSize.Width, Center.Y));
                    g.DrawLine(DrawPen, Center.X, Center.Y, Center.X, Height - 1);
                }
                else
                {
                    g.DrawLines(DrawPen, IntToPoints(0, Center.Y, Center.X - SlashSize.Width, Center.Y, Center.X + SlashSize.Width, Center.Y - SlashSize.Height));
                    g.DrawLine(DrawPen, Center.X + SlashSize.Width, Center.Y, Width - 1, Center.Y);
                }
            }
        }

        /// <summary>
        /// Draw the line text
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextLabel_Paint(object sender, PaintEventArgs e)
        {
            if (DisplayParameters.Names)
                e.Graphics.DrawString(this.Name, SystemFonts.CaptionFont, Brushes.Aqua, PointF.Empty);
        }
        #endregion
    }
}
